package com.dsx.myblog.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dsx
 */
@Configuration
public class RedisCacheManagerCustomizer {
//    @Bean
//    public CacheManagerCustomizer<RedisCacheManager> cacheManagerCustomizer(){
//        return new CacheManagerCustomizer<RedisCacheManager>() {
//            @Override
//            public void customize(RedisCacheManager cacheManager) {
//                Map<String, Long> expires = new HashMap<>();
//                expires.put("menu", 60L);
////                cacheManager.
//            }
//        };
//    }
    @Autowired
    private RedisConnectionFactory connectionFactory = null;

    @Bean(name="redisCacheManager")
    public RedisCacheManager initRedisCacheManager(){
        RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new JdkSerializationRedisSerializer()));
        config = config.disableKeyPrefix().entryTtl(Duration.ofMinutes(10));
        RedisCacheManager redisCacheManager = new RedisCacheManager(writer,config);
        return redisCacheManager;

    }



}
